#!/usr/local/bin/php -q
<?php
require ('../conf/config.inc');
require_once COMMON_LIB.'DVS/DVS.php';
require_once COMMON_LIB.'DVS/Dynamic.php';

require_once (PROJECT_ROOT.'/layout/saveData.php');

set_time_limit (7200);


$dir = PROJECT_ROOT.'data/'.date('Ymd').'_villa';


//$file_arr = saveData::getFilesArray($dir);
/*
foreach ($file_arr as $k => $file) {
    echo $file;
}
*/
$file = PROJECT_ROOT.'WWW/p437431.htm';

//$str = file_get_contents($dir.'/'.$file);
$str = file_get_contents($file);

//echo $str;
//$id_tpl = '/href="\/p(\d+)" rel="nofollow"><img src="([^"]+)"/';
//preg_match_all ($regions_tpl, $str, $regions_match);

/*
    [1] => Title
    [2] => Summary
    [3] => Region_id
    [4] => Bedroom
    [5] => Sleeps
    [6] => Bathroom
    [7] => Type (trim)
    [8] => Summary foto
    [9] => Description foto
    [10] => a
    [11] => b
    [12] => Notes
    [13] => Extra
*/

//$villa_tpl = '/<h1>([^<]+)<\/h1>[\n\t]+<h2>([^<]+)<\/h2>[^0-9]+(\d+)[^"]{0,}"[\s\S]+?Bedrooms<\/span><span class="count">([^<]+)<.*?Sleeps<\/span><span class="count">([^<]+).*?<li>([^<]+?)<\/li>.*?class="title">Description<\/span>\s?([^<]+)<[\s\S]+?class="summary">([^<]+)<[\s\S]+?class="prop-desc-txt">([^<]+)<.*?{a:([^,]+), b:([^,]+).*?class="doubleColumn notes">([^<]+)<.*?id="propertyDetailsContent">([\s\S]+?)<\/div>/s';

$region_id_tpl = '/&gt;[^\d]+?(\d+)[^&]+?&nbsp;/';

$villa_tpl = '/<h1>([^<]+)<\/h1>[\n\t]+<h2>([^<]+)<\/h2>[^0-9]+(\d+)[^"]{0,}"[\s\S]+?Bedrooms<\/span><span class="count">([^<]+)<.*?Sleeps<\/span><span class="count">([^<]+).*?<li>([^<]+?)<\/li>.*?class="title">Description<\/span>\s?([^<]+)<[\s\S]+?class="summary">([^<]+)<[\s\S]+?class="prop-desc-txt">([^<]+)<.*?{a:([^,]+), b:([^,]+)/s';

$notes_tpl = '/class="doubleColumn notes">([^<]+)</';

$extra_tpl = '/id="propertyDetailsContent">([\s\S]+?)<\/div>/s';

$photo_tpl = '/"photo-div">[\n\t]{0,}<img src="([^"]+)".*alt="([^"]+)"/';

$rate_tpl = '/class="period">([^<]+)<[\s\S]+?class="rate">([^\d]+)(\d+) -[\n\t\s]+([^\d]+)(\d+)</';

$facility_tpl = '/class="firstColumn">([^:]+):(.*?)<\/div>/s';
// Notes
//$facility2_tpl = '/(<li>|<span class="doubleColumn notes">)\s{0,}(.*?)\s{0,}<\//s'
//witout Notes
$facility2_tpl = '/<li>\s{0,}(.*?)\s{0,}<\//s';


echo '<pre>';


preg_match ("/p(\d+)[^\d]?/", $file, $id_match);

$id = $id_match[1];

echo $id.'<br><br>';
preg_match_all ($region_id_tpl, $str, $region_id_match);
print_r($region_id_match);

preg_match ($villa_tpl, $str, $match);
preg_match ($notes_tpl, $str, $notes_match);
preg_match ($extra_tpl, $str, $extra_match);
//print_r($match);

preg_match_all ($photo_tpl, $str, $photo_match);
preg_match_all ($rate_tpl, $str, $rate_match);
preg_match_all ($facility_tpl, $str, $facility_match);

if ($facility_match) {
    array_shift($facility_match);
    //print_r($facility_match);
    foreach ($facility_match[0] as $k => $v) {
        preg_match_all($facility2_tpl, $facility_match[1][$k], $options_match);
        //array_shift($options_match);
        array_shift($options_match);
        $opt_arr[$v] = $options_match[0];
    }
}
//print_r($opt_arr);

if ($match) {
$match[0] = $id;
array_shift($notes_match);
array_shift($extra_match);
array_shift($photo_match);
array_shift($rate_match);
array_shift($region_id_match);
array_shift($region_id_match[0]);
$match['region_id'] = $region_id_match[0];
$match['notes'] = $notes_match;
$match['extra'] = $extra_match;
$match['img'] = $photo_match;
$match['rate'] = $rate_match;
$match['opt'] = $opt_arr;
}

print_r($match);
//exit;
//print_r($photo_match);
//print_r($rate_match);

$insert = 1;

if ($match && $insert) {
    $save_obj = new saveData;
    $db_obj = DVS_Dynamic::createDbObj('villa');
    DB_DataObject::DebugLevel(1);
    $save_obj->insertVilla($match, $db_obj);
}

?>